﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.Administration;
using IdeasFree.SharePoint.StsAdm.Logger;
using System.Reflection;


namespace IdeasFree.SharePoint.StsAdm.SPJobs
{
    /// <summary>
    /// JobDefinition to run artifact deployment.
    /// </summary>
    public class DeployResourceJobDefinition : SPJobDefinition
    {
        public DeployResourceJobDefinition()
            : base()
        {

        }

        public DeployResourceJobDefinition(string jobName, SPService service, SPServer server, SPJobLockType lockType)
            : base(jobName, service, server, lockType)
        {
            this.Title = jobName;
        }

        public DeployResourceJobDefinition(string jobName, ILogger eventLogger,SPWebApplication webApplication, SPJobLockType lockType)
            : base(jobName, webApplication, null, lockType)
        {
            this.Title = jobName;
        }

        public override void Execute(Guid targetInstanceId)
        {
            ILogger _eventLogger = Tools.Instance.EventLogger;
            try
            {
                SPWebApplication spWebApplication = this.Parent as SPWebApplication;
                spWebApplication.WebService.ApplyApplicationContentToLocalServer();
                Type type = typeof(ILogger);

                if (_eventLogger != null)
                    _eventLogger.Log("Running Resource Deployment at : {0}.", System.Diagnostics.EventLogEntryType.Information, DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss"));
            }
            catch (Exception exp)
            {
                if (_eventLogger != null)
                    _eventLogger.Log("Error: Running Resource Deployment.", exp, System.Diagnostics.EventLogEntryType.Error);               
            }
        }
    }
}
